package com.lagou.edu.controller;

import com.lagou.edu.constant.RequestConstant;
import com.lagou.edu.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author srh
 * @date 2020/03/20
 **/
@Controller
@RequestMapping(value = "/account")
public class AccountController {

    @Autowired
    private ResumeController resumeController;

    @RequestMapping(value = "/login")
    public ModelAndView login(ModelAndView modelAndView, HttpSession session, Account account) {
        if (null == account) {
            modelAndView.addObject(RequestConstant.ERROR_MSG, "暂无用户登陆信息请重新填写");
            modelAndView.setViewName(RequestConstant.LOGIN);
        } else if (!RequestConstant.USERNAME.equals(account.getUsername())) {
            modelAndView.addObject(RequestConstant.ERROR_MSG, "当前账号不存在");
            modelAndView.setViewName(RequestConstant.LOGIN);
        } else if (!RequestConstant.PASSWORD.equals(account.getPassword())) {
            modelAndView.addObject(RequestConstant.ERROR_MSG, "密码输入错误");
            modelAndView.setViewName(RequestConstant.LOGIN);
        } else {
            session.setAttribute(RequestConstant.TOKEN, account);
            return resumeController.findAll(modelAndView);
        }
        return modelAndView;
    }

}
